"""
Copyright (c) 2022 Guangdong University of Technology
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Meng Xiang, Junjiang Xiang
Created: 2023/8/19
Supported by: National Key Research and Development Program of China
"""

from .hybrid import hybrid
from .pd import pd


def coherent_receiver(input, Optical_LO, Responsivity):
    """相干接收机
        Args:
            input[0]: 输入X偏振信号，numpy类型
            input[1]: 输入Y偏振信号，numpy类型
            Optical_LO: 光信号
            Responsivity: 响应度

        Returns:
            output[0]：输出X偏振信号
            output[1]：输出Y偏振信号
        """
    TxSignal_X = input[0]
    TxSignal_Y = input[1]

    Optical_LO = Optical_LO.reshape((-1, 1))

    # X pol
    signalOutput0grad, signalOutput180grad, signalOutput90grad, signalOutput270grad = hybrid(TxSignal_X,
                                                                                                          Optical_LO)
    output_electrical_signal_1 = pd(signalOutput0grad, Responsivity)  # PD探测
    output_electrical_signal_2 = pd(signalOutput180grad, Responsivity)
    output_electrical_signal_3 = pd(signalOutput90grad, Responsivity)
    output_electrical_signal_4 = pd(signalOutput270grad, Responsivity)
    output_i = output_electrical_signal_1 - output_electrical_signal_2  # 平衡接收
    output_q = output_electrical_signal_3 - output_electrical_signal_4
    TxSignal_X = output_i + 1j * output_q  # 合成复数信号
    # Y pol
    [signalOutput0grad, signalOutput180grad, signalOutput90grad, signalOutput270grad] = hybrid(TxSignal_Y,
                                                                                                            Optical_LO)
    output_electrical_signal_1 = pd(signalOutput0grad, Responsivity)
    output_electrical_signal_2 = pd(signalOutput180grad, Responsivity)
    output_electrical_signal_3 = pd(signalOutput90grad, Responsivity)
    output_electrical_signal_4 = pd(signalOutput270grad, Responsivity)
    output_i = output_electrical_signal_1 - output_electrical_signal_2
    output_q = output_electrical_signal_3 - output_electrical_signal_4
    TxSignal_Y = output_i + 1j * output_q

    output = [TxSignal_X, TxSignal_Y]

    return output
